/*
 * author : Dinh Ngoc Tuan Vu (A0092633)
 */
package sg.edu.nus.iss.team4ft.service;

import java.io.Serializable;


import java.util.ArrayList;
import java.util.Date;

import sg.edu.nus.iss.team4ft.exception.InvalidOperationException;
import sg.edu.nus.iss.team4ft.util.StringUtil;

public class CustomerAccount implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Customer customer;
	private Account account;
	private ArrayList<CustomerAccount> listCustomers = new ArrayList<CustomerAccount>();
		
	public CustomerAccount(){
		
	}
	
	public CustomerAccount(String firstName, String lastName, String NRIC, Address address, long contactNumber, String interest){
		setCustomer(new Customer(firstName, lastName, NRIC, address, contactNumber, interest));
		setAccount(new Account());
	}
	
	public CustomerAccount addCustomerToList(String firstName, String lastName, String NRIC, Address address, long contactNumber, String interest){
		CustomerAccount customer = new CustomerAccount(firstName, lastName, NRIC, address, contactNumber, interest);
		listCustomers.add(customer);
		return customer;
	}	

	public ArrayList<CustomerAccount> getCustomers(){
		ArrayList<CustomerAccount> temp = new ArrayList<CustomerAccount>();
		for(CustomerAccount ca : listCustomers){
			if(ca != null){
				temp.add(ca);
			}			
		}		
		return temp;
	}
	
	public Customer getCustomer() throws CloneNotSupportedException {
		try {
			if(customer!= null){
				return customer.clone();
			}else{
				return null;
			}
		} catch (CloneNotSupportedException e) {
			throw e;
		}
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Account getAccount() throws CloneNotSupportedException {
		try {
			if(account != null)
				return account.clone();
		} catch (CloneNotSupportedException e) {
			throw e;
		}
		return null;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	
	@Override
	protected CustomerAccount clone() throws CloneNotSupportedException {
		CustomerAccount ca = new CustomerAccount();
		ca.customer = this.customer.clone();
		ca.account = this.account.clone();
		return ca;
	}

	public void deregisterCustomer() throws InvalidOperationException {
		this.customer.setStatus(false);
		this.account.deregister();		
	}
	
	public boolean equals(CustomerAccount customerAccount) throws CloneNotSupportedException{
		if (this.getAccount().equals(customerAccount.getAccount()) && this.getCustomer().equals(customerAccount.getCustomer())){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public String toString() {
		//return this.customer.getCustomerId() + "      " + this.account.getAccountNumber() + "       " + this.customer.getFirstName() + "                 " + this.customer.getLastName() + "              " + this.customer.getTelephoneNumber();
		if(this.account != null && this.customer != null)
		{	
			String lineNum = "-------------------";
			String mobileNum = "-------------------";
			String contactNum = "-------------------";
			
			if(this.customer.getTelephoneNumber() != null)
				contactNum = this.customer.getTelephoneNumber().toString();
			
			for (Plan p : this.account.getPlans()) {
				if(p instanceof MobileVoice && p.getEndDate() == null)
					mobileNum = ((MobileVoice)p).getTelephoneNumberInString();

				if(p instanceof DigitalVoice && p.getEndDate() == null)
					lineNum = ((DigitalVoice)p).getTelephoneNumberInString();
			}
			return this.customer.getCustomerId() + StringUtil.getEmptySpace(this.customer.getCustomerId())  + this.account.getAccountNumber() 
					+ StringUtil.getEmptySpace(this.account.getAccountNumber() ) + this.customer.getName() + StringUtil.getEmptySpace(this.customer.getName()) 
					+ contactNum + StringUtil.getEmptySpace(contactNum) 
					+ mobileNum + StringUtil.getEmptySpace(mobileNum) +lineNum + StringUtil.getEmptySpace(lineNum) + (this.customer.isActive() != null && this.customer.isActive() ? "Active" : "Inactive");
		}
		return "";
	}
	
	public boolean isActive(Date forMonth) {
		boolean isActive = false;
		//Check if the subscription is made before the date
		for (Plan plan : account.getPlans()) {
			if(!isActive && plan.isSubscriptionActive(forMonth))
				isActive = true;
		}
		
		return isActive;		
	}

	public void activateCustomer() {
		this.customer.setStatus(true);
		
	}
}
